﻿using UnityEngine;
using GraphProcessor;

namespace IQIGame.Onigao.GamePlay.Editors
{
    [System.Serializable, NodeMenuItem(LevelEditorConst.Level + "/" + LevelEditorConst.LevelService, typeof(LevelServiceGraph))]
    public class LevelServiceNode : LevelServiceBaseNode
    {
        public override bool isRenamable => true;
        public override Color color => Color.green;

        [Input("开始", false), Vertical] public EditorLevelServiceConfig serveiceGroup;
        [Output("下一服务", false), Vertical] public EditorLevelServiceConfig next;
        [Output("执行", false)] public LevelServiceEditor execution;
        public override string name => LevelEditorConst.LevelService;

        [InspectorName("默认关闭")]public bool defaultDisable;
        [InspectorName("延时执行（毫秒）")]public int delay;
        [InspectorName("储存延时")]public bool saveDelay;
        [LevelServiceEditorCustomProperty("Se", "打断类型")]public int breakType;

        protected override void Process()
        {
            if (this.serveiceGroup == null)
            {
                return;
            }
            base.Process();
            var rServiceConfig = new EditorLevelServiceUnitConfig()
            {
                delay = this.delay,
                serviceBreak = new EditorLevelServiceBreakConfig()
                {
                    breakType = this.breakType
                },
                name = this.GetCustomName(),
                defaultDisable = this.defaultDisable,
                saveDelay = this.saveDelay
            };
            this.execution = new LevelServiceEditor()
            {
                serviceConfig = rServiceConfig
            };
            this.serveiceGroup.serviceUnits.Add(rServiceConfig);
            this.next = this.serveiceGroup;
        }
    }

    public class LevelServiceEditor
    {
        public int conditionType;
        public EditorLevelServiceUnitConfig serviceConfig;
        public EditorLevelServiceExecPrefabSource executionPrefab;

        public void AddExectuion(EditorLevelServiceExecutionConfig rExecution)
        {
            if (this.serviceConfig != null)
            {
                var rServerConfig = this.serviceConfig;
                switch (this.conditionType)
                {
                    // 强制执行
                    case 0:
                        rServerConfig.preExecution.Add(rExecution);
                        break;
                    // 条件符合执行
                    case 1:
                        rServerConfig.execution.Add(rExecution);
                        break;
                    // 条件不符执行
                    case -1:
                        rServerConfig.vetoExecution.Add(rExecution);
                        break;
                }
            }
            else if(executionPrefab != null)
            {
                this.executionPrefab.executions.Add(rExecution);
            }
        }
    }
}